package com.lzc.blog.controller;

import com.lzc.blog.common.aop.LogAnnotation;
import com.lzc.blog.common.cache.Cache;
import com.lzc.blog.vo.Result;
import com.lzc.blog.vo.params.ArticleParam;
import com.lzc.blog.vo.params.PageParams;
import com.lzc.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;


//Rest风格
@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    /**
     * 首页，文章列表
     * @param pageParams
     * @return
     */

    @PostMapping
    //加上此接口，代表要对此接口记录日志
    @LogAnnotation(module="文章",operator="获取文章列表")
    @Cache(expire = 5 * 60 * 1000,name = "listAarticle")
    public Result listArticle(@RequestBody PageParams pageParams){
        return articleService.listArticle(pageParams);
    }

    /**
     * 首页 最热文章
     * @return
     */
    @PostMapping("/hot")
    @Cache(expire = 5 * 60 * 1000,name = "hot_article")
    public Result hotArticle(){
        int limit=5;
        return articleService.hotArticle(5);
    }

    /**
     * 首页 最新文章
     * @return
     */
    @PostMapping("/new")
    @Cache(expire = 5 * 60 * 1000,name = "news_article")
    public Result newArticle(){
        int limit=5;
        return articleService.newArticle(limit);
    }

    /**
     * 首页 文章归档
     * @return
     */
    @PostMapping("/listArchives")
    public Result listArchives(){
        return articleService.listArchives();
    }

    /**
     * 查看文章
     * @param articleId
     * @return
     */
    @PostMapping("/view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }

    /**
     * 修改文章时回显数据
     * @param articleId
     * @return
     */
    @PostMapping("/{id}")
    public Result findArticleByIdToShow(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }

    /**
     * 发布文章
     * @param articleParam
     * @return
     */
    @PostMapping("/publish")
    public Result publish(@RequestBody ArticleParam articleParam){

        return articleService.publish(articleParam);
    }

    /**
     * 删除文章
     * @param articleId
     * @return
     */
    @CrossOrigin(origins = "*")
    @DeleteMapping ("/delete/{id}")
    public Result deleteById(@PathVariable("id") Long articleId){
        return articleService.deleteById(articleId);
    }
}
